package com.example.vv.model.setupTreeList.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.vv.R;
import com.example.vv.fragmentWork.entity.ArticleListEntity;
import com.example.vv.model.projectItemDetails.PIDActivity;
import com.example.vv.model.setupTreeListDetails.STLDActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class STLAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<ArticleListEntity.DatasBean> datasBeans = new ArrayList<>();
    Context context;


    public STLAdapter(Context context) {

        this.context = context;
    }

    class STDHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.stl_author)
        TextView stlAuthor;
        @BindView(R.id.stl_time)
        TextView stlTime;
        @BindView(R.id.stl_type)
        TextView stlType;
        @BindView(R.id.stl_content)
        TextView stl_content;


        public STDHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void clear(){
        datasBeans.clear();
    }

    public void addData(List<ArticleListEntity.DatasBean> list) {
        datasBeans.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_stl_list, parent, false);
        STDHolder holder = new STDHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        STDHolder holder1 = (STDHolder) holder;
        holder1.stlTime.setText(datasBeans.get(position).getNiceDate());
        holder1.stlAuthor.setText(datasBeans.get(position).getAuthor().equals("")?datasBeans.get(position).getShareUser():datasBeans.get(position).getAuthor());
        holder1.stlType.setText("分类:"+datasBeans.get(position).getChapterName());
        holder1.stl_content.setText(datasBeans.get(position).getTitle());
        holder1.stl_content.setOnClickListener(v->{
            EventBus.getDefault().postSticky(datasBeans.get(position));
            context.startActivity(new Intent(context, STLDActivity.class));
        });
        /*holder1.stlType.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(datasBeans.get(position));
            context.startActivity(new Intent(context, PIDActivity.class));
        });*/
    }

    @Override
    public int getItemCount() {
        return datasBeans.size() == 0 ? 0 : datasBeans.size();
    }
}
